package com.gzbugu.bugusys.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gzbugu.bugusys.bean.PageModel;
import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.service.ITrafficContractService;
import com.gzbugu.bugusys.util.IHQLQuery;
import com.gzbugu.bugusys.vo.TrafficContract;

/**
 * 增删改查业务合同service
 * 
 * @author gauze
 * 
 */
@Transactional
@Service
public class TrafficContractService implements ITrafficContractService {

	@Resource(name = "hQLQuery")
	IHQLQuery hQLQuery;
	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.bugusys.service.impl.ITrafficContractService#addContract(com
	 * .gzbugu.bugusys.vo.TrafficContract)
	 */
	public boolean addContract(TrafficContract contract) {
		baseDAO.save(contract);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.bugusys.service.impl.ITrafficContractService#deleteContract
	 * (java.lang.Integer)
	 */
	public boolean deleteContract(Integer contractId) {
		TrafficContract contract = (TrafficContract) baseDAO.findById(
				contractId,
				com.gzbugu.bugusys.fields.VoClasseFields.TRAFFIC_CONTRACT);
		if (contract != null) {
			File file = new File(contract.getUrl());
			try {
				FileUtils.forceDelete(file);
			} catch (IOException e) {
				// 不处理
			}
			baseDAO.delete(contract);
			return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.bugusys.service.impl.ITrafficContractService#alterContract
	 * (java.lang.Integer, com.gzbugu.bugusys.vo.TrafficContract)
	 */
	public TrafficContract alterContract(Integer contractId,
			TrafficContract newContract) {
		TrafficContract contract = (TrafficContract) baseDAO.findById(
				contractId,
				com.gzbugu.bugusys.fields.VoClasseFields.TRAFFIC_CONTRACT);
		if (contract != null) {
			// 设置contract信息
			contract.setAmount(newContract.getAmount());
			contract.setCustomer(newContract.getCustomer());
			contract.setDate(newContract.getDate());
			contract.setEnd(newContract.getEnd());
			contract.setName(newContract.getName());
			contract.setStart(newContract.getStart());
			contract.setSummarize(newContract.getSummarize());
			contract.setUrl(newContract.getUrl());
			baseDAO.attachDirty(contract);// 保存
			return contract;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.bugusys.service.impl.ITrafficContractService#findContract(
	 * com.gzbugu.bugusys.bean.PageModel)
	 */
	public List findContract(PageModel page) {
		String hql = "from TrafficContract";
		return hQLQuery.find(hql, page);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.bugusys.service.impl.ITrafficContractService#findContract()
	 */
	public List findContract() {
		String hql = "from TrafficContract";
		return hQLQuery.find(hql);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.gzbugu.bugusys.service.ITrafficContractService#countContract()
	 */
	public int countContract() {
		String hql = "select count(*) from TrafficContract";
		return ((Long) hQLQuery.find(hql).get(0)).intValue();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.bugusys.service.ITrafficContractService#saveUploadfile(java
	 * .lang.String, java.io.File)
	 */
	public File saveUploadfile(String uploadFileName, File upload) {
		if (uploadFileName == null || upload == null)
			return null;
		// 合同文件保存在根目录下的filesources/trafficContract文件夹下
		File contractFiles = new File(ServletActionContext.getServletContext()
				.getRealPath("/")
				+ "filesources" + File.separator + "trafficContract");
		// 合同文件保存文件夹不存在时，新建文件夹
		if (!contractFiles.exists())
			contractFiles.mkdirs();
		// 保存合同文件
		File saveFile = new File(contractFiles.getAbsolutePath()
				+ File.separator + uploadFileName);
		try {
			FileUtils.copyFile(upload, saveFile);
		} catch (IOException e) {
			return saveFile;
		}
		return saveFile;
	}

}
